package com.lanou.pianke.admin.web.controller;

import com.lanou.pianke.admin.entity.Pcomment;
import com.lanou.pianke.admin.entity.Preply;
import com.lanou.pianke.admin.service.PcommentService;
import com.lanou.pianke.admin.service.PreplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/")
public class PcommentController {
    @Autowired
    private PcommentService pcommentService;

    @Autowired
    private PreplyService preplyService;


    // 显示所有评论和回复
//    @GetMapping("showPcommentsAndPreply")
//    public List<Pcomment> showPcommentsAndPreply(){
//        List<Pcomment> pcommentList = pcommentService.showAll();
//        for (Pcomment pcomment : pcommentList) {
//            String pcommentId = pcomment.getPcommentId();
////            System.out.println("pcommentId=========" + pcommentId);
//            // 通过评论id查回复集合
//            List<Preply> preplyList = preplyService.showAllByPcommentId(pcommentId);
//            if (preplyList != null){
//               pcomment.setPreplyList(preplyList);
//            }
//        }
//        return pcommentList;
//    }

    // 显示所有被举报数>10的评论和回复
    @GetMapping("showCommentReported")
    public List<Pcomment> showCommentReported(){
        List<Pcomment> pcommentList = pcommentService.showReported();
        for (Pcomment pcomment : pcommentList) {
            String pcommentId = pcomment.getPcommentId();
//            System.out.println("pcommentId=========" + pcommentId);
            // 通过评论id查回复集合
            List<Preply> preplyList = preplyService.showAllByPcommentId(pcommentId);
            if (preplyList != null){
                pcomment.setPreplyList(preplyList);
            }
        }
        return pcommentList;
    }

    // 删除所有被举报评论
    @DeleteMapping("deletePcomment")
    public void deletePcomment(){
        pcommentService.deleteCommentByReported();
    }

    // 删除某个评论与该评论下的回复
    @DeleteMapping("deleteByPcommentId/{pcommentId}")
    public void deleteByPcommentId(@PathVariable String pcommentId){
        pcommentService.deletePcomment(pcommentId);
        // 通过评论id查回复集合
        List<Preply> preplyList = preplyService.showAllByPcommentId(pcommentId);
        if (preplyList != null){
            // 根据评论id删除该评论下的回复
            preplyService.deletePreply(pcommentId);
        }
    }


}
